<?php

class Email
{
	var $error;

	function get_contacts($account, $password)
	{
		$contacts = grap_sina($account, $password);
		if (!is_array($contacts)) {
			$this->error = $contacts;
			return false;
		}
		return $contacts;
	}

	function get_error()
	{
		return $this->error;
	}
}


/**
 * grap contacter_list from mail.sina.com
 * create by liuzhiguo clayton.liu@gmail.com
 * 2008-8-20 16:15 1.0 $
 */

/*
$user = trim($_POST['username']);
$password = trim($_POST['password']);

$contacter_list = grap_sina($user, $password);
//header("Content-Type: text/html; charset=utf-8");
print_r($contacter_list);
*/
/**
 * grap contacters from mail.sina.com
 *
 * @param string $user
 * @param string $password
 * @return array $contacter_list
 */
function grap_sina($user, $password){
	if (strpos($user, '@') !== false) {
		$user = current(explode('@', $user));
	}

	$tmp_dir = PATH_SEPARATOR == ':' ? '/dev/shm/invite/' : 'c:/windows/temp/';
	$tmp_dir = $tmp_dir . substr(md5(microtime(true) . rand(0, 999999999)), 0, 2);

	$cookie_jar = tempnam($tmp_dir, 'cookie');
	$cookie_jar2 = tempnam($tmp_dir, 'cookie2');

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_TIMEOUT, 3);
	curl_setopt($ch, CURLOPT_URL, 'http://mail.sina.com.cn/cgi-bin/login.cgi');
	curl_setopt($ch, CURLOPT_POST, 1);
	$request = 'logintype=uid&u='.$user.'&psw='.$password.'&btnloginfree=%B5%C7+%C2%BC';
	curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_NOBODY, false);
	$html=curl_exec($ch);
	curl_close($ch);

	$lines = explode("\n", $html);
	$location = '';
	foreach ($lines as $line) {
		if (preg_match('/^Location:/', $line)) {
			$location = substr($line, 9);
			$location = trim($location);
		}
	}

	if (!$location) {
		unlink($cookie_jar);
		unlink($cookie_jar2);
		return 'err_01';
	}

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_TIMEOUT, 3);
	curl_setopt($ch, CURLOPT_URL, $location);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar);
	curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar2);
	$html2 = curl_exec($ch);
	curl_close($ch);


	//get data after login
	$host = parse_url($location, PHP_URL_HOST);
	$ch2 = curl_init();
	curl_setopt($ch2, CURLOPT_TIMEOUT, 3);
	curl_setopt($ch2, CURLOPT_URL, 'http://' . $host . '/classic/addr_member.php');
	curl_setopt($ch2, CURLOPT_POSTFIELDS, 'act=list&sort_item=letter&sort_type=desc');
	curl_setopt($ch2, CURLOPT_HEADER, false);
	curl_setopt($ch2, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie_jar2);
	$json_data = curl_exec($ch2);

	unlink($cookie_jar);
	unlink($cookie_jar2);

	$json_data = json_decode($json_data, true);
	if ($json_data['result'] != 1 || $json_data['data']['total'] == 0) {
		return 'err_02';
	}

	$contacts = array();
	foreach ($json_data['data']['contact'] as $k => $v) {
		$tmp = array();
		$tmp['name'] = iconv('UTF-8', 'GBK', $v['name']);
		$tmp['email'] = iconv('UTF-8', 'GBK', $v['email']);
		array_push($contacts, $tmp);
	}
	return $contacts;
}
?>